<template>
  <div class="mystery">
    <div class="common">
      <div class="common-input">
        <el-input
          v-model="commonValue"
          placeholder="盲盒名称"
          clearable
        ></el-input>
      </div>

      <div class="common-button" @click="searchHit">搜索</div>
      <div
        class="common-button"
        style="background: #999"
        @click="replacementHit"
      >
        重置
      </div>
      <div class="common-button" @click="addHit" style="background: #2a82e4">
        新增
      </div>
    </div>

    <div class="table" v-loading="loading">
      <el-table
        tooltip-effect="dark"
        ref="multipleTable"
        :data="tableData"
        style="width: 100%"
        border
        @selection-change="handleSelectionChange"
        :header-cell-style="{
          color: '#333333',
          backgroundColor: '#f3f3f3',
        }"
      >
        <el-table-column type="selection" width="70" align="center">
        </el-table-column>
        <el-table-column prop="" label="盲盒图片" width="150">
          <template slot-scope="scope">
            <img class="imgs" :src="scope.row.shopBlindBoxIcon" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="shopBlindBoxName" label="盲盒名称" width="300">
        </el-table-column>
        <el-table-column
          prop="retailPrice"
          label="盲盒销售价（元）"
          width="200"
        >
        </el-table-column>
        <el-table-column
          prop="lineationPrice"
          label="盲盒原件（元）"
          width="200"
        ></el-table-column>
        <el-table-column prop="shopBlindBoxDesc" label="盲盒包" width="230">
        </el-table-column>
        <el-table-column label="是否上架" width="200">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isStatus == -1" type="info">草稿</el-tag>
            <el-tag v-if="scope.row.isStatus == 0">未上架</el-tag>
            <el-tag v-if="scope.row.isStatus == 1" type="success"
              >已上架</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="inventoryNum" label="库存" width="200">
        </el-table-column>
        <el-table-column prop="saleNum" label="销量" width="200">
        </el-table-column>
        <el-table-column prop="createTime" label="操作时间" width="250">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="250">
          <template slot-scope="scope">
            <div class="operation">
              <div class="list" @click.prevent="editHit(scope.row)">编辑</div>
              <div class="list" @click.prevent="deleteHit(scope.row)">删除</div>
            </div>
          </template></el-table-column
        ></el-table
      >

      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          layout="prev, pager, next"
          :total="tableDataTotal"
          :current-page="page"
        >
        </el-pagination>
      </div>
    </div>

    <!--删除弹出框-->
    <div class="delete">
      <el-dialog
        title="删除"
        :visible.sync="deleteVisible"
        :before-close="deleteClose"
      >
        <div class="delete-title">是否确认删除？</div>
        <div class="delete-attachment">
          <div class="list" @click="deleteClose">取消</div>
          <div class="list" @click="deleteConfirm">确定</div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { boxBlindList, boxBlindListDelete } from "@/api/mystery";

export default {
  data() {
    return {
      commonValue: "",
      //-------------
      page: 1,
      limit: 10,
      tableData: [],
      tableDataTotal: 0,
      loading: false,
      commonObj: {},
      multipleSelection: [],
      //-----------
      deleteVisible: false,
    };
  },
  created() {
    this.loading = true;
    this.boxBlindListMet();
  },
  methods: {
    boxBlindListMet() {
      let params = {
        keyWord: "",
        pageSize: this.limit,
        pageNo: this.page,
      };

      boxBlindList(params).then((res) => {
        if (res.errorCode == null) {
          res.resultData.blindBoxList.forEach((item, index) => {
            item.retailPrice = Number(item.retailPrice / 100).toFixed(2);
            item.lineationPrice = Number(item.lineationPrice / 100).toFixed(2);
          });
          this.tableData = res.resultData.blindBoxList;
          this.tableDataTotal = res.resultData.totalNum;
          this.loading = false;
        }
      });
    },
    //------------------------------------------------
    searchHit() {
      this.loading = true;
      this.boxBlindListMet();
    },
    replacementHit() {
      this.loading = true;
      this.boxBlindListMet();
    },
    //------------------------------------------------
    addHit() {
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error("暂无权限，请联系管理员开通");
        return;
      }

      this.$router.push({
        path: "/mystery/add",
        query: {
          source: 1,
        },
      });
    },
    editHit(item) {
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error("暂无权限，请联系管理员开通");
        return;
      }

      this.$router.push({
        path: "/mystery/add",
        query: {
          source: 1,
          id: item.shopBlindBoxId,
        },
      });
    },
    //----------------
    deleteHit(item) {
      if (this.$store.state.premissions.infoRouterShow == false) {
        this.$message.error("暂无权限，请联系管理员开通");
        return;
      }

      this.commonObj = item;
      this.deleteVisible = true;
    },
     deleteClose() {
      this.deleteVisible = false;
    },
    deleteConfirm() {
      let params = this.commonObj.shopBlindBoxId;

      boxBlindListDelete(params).then((res) => {
        if (res.errorCode == null) {
          this.$message.success("删除成功！");
          this.loading = true;
          this.boxBlindListMet();
          this.deleteVisible = false;
        }
      });
    },
    //----------------------
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //---------
    handleSizeChange(val) {
      this.limit = val;
      this.loading = true;
      this.boxBlindListMet();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.loading = true;
      this.boxBlindListMet();
    },
  },
};
</script>

<style lang="less" scoped>
@import url("@/assets/css/common.less");
@import url("@/assets/css/delete.less");

.mystery {
  .add {
    /deep/ .el-dialog__header {
      font-size: 16px;
      text-align: center;
    }

    /deep/ .el-dialog__body {
      padding-bottom: 10px;
    }

    .add-form {
      /deep/ .el-select {
        width: 100%;
      }

      .img {
        width: 70px;
        height: 70px;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid #ccc;
        border-radius: 5px;

        img {
          width: 100%;
          height: 100%;
          border-radius: 5px;
        }
      }

      .attachment {
        margin-top: 50px;
        /deep/ .el-form-item {
          margin-bottom: 0;
        }

        /deep/ .el-form-item__content {
          margin-left: 0 !important;
          display: flex;
          align-items: center;
          justify-content: center;
        }

        .list {
          background: #3da8f5;
          color: #fff;
          font-size: 16px;
          margin-right: 50px;
          border-radius: 5px;
          width: 100px;
          height: 35px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }

        .list:first-child {
          background: #808080;
        }

        .list:last-child {
          margin-right: 0;
        }
      }
    }
  }
}
</style>